﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_NNKDCoDieuKien
    Inherits System.Web.UI.UserControl


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'xu ly jquery khi post back
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            Bind2Grid()
        End If
    End Sub

    Protected Sub Bind2Grid()

        Using _data As New VSEntities
            Dim p = (From q In _data.Menus Where q.TitlePage = "nganh-nghe-kinh-doanh-co-dieu-kien" Select q).FirstOrDefault
            If Not p Is Nothing Then
                Dim k = (From q In _data.Menus Where q.ParentId = p.MenuId Select q).ToList
                With rptNganhNghePhoBien
                    .DataSource = k
                    .DataBind()
                End With
            End If
            If Request("Id") <> "" Then
                Dim _menuId As Integer = Request("Id")
                Dim k = (From q In _data.Menus Where q.MenuId = _menuId Select q).ToList
                With rptKetQua
                    .DataSource = k
                    .DataBind()
                End With
            End If
        End Using

    End Sub

    Protected Sub rptNganhNghePhoBien_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptNganhNghePhoBien.ItemDataBound
        Dim row As Menu = e.Item.DataItem
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & "nganh-nghe-kinh-doanh-co-dieu-kien.html?Id=" & row.MenuId
        hplTitle.Text = row.Title
    End Sub

    Protected Sub btnFilterNganhNghe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilterNganhNghe.Click
        Using _data As New VSEntities


            Dim p = (From q In _data.Menus Where q.TitlePage = "nganh-nghe-kinh-doanh-co-dieu-kien" Select q).FirstOrDefault
            If Not p Is Nothing Then

                Dim k = (From q In _data.Menus Where q.ParentId = p.MenuId Where (q.Title.Contains(txtCode.Text) And txtCode.Text <> "") Or (q.Title.Contains(txtName.Text) And txtName.Text <> "") _
                                                                                 Or (q.Description.Contains(txtCode.Text) And txtCode.Text <> "") Or (q.Description.Contains(txtName.Text) And txtName.Text <> "")
                                                                                  Select q Order By q.Title Descending).ToList

                With rptKetQua
                    .DataSource = k
                    .DataBind()
                End With
            End If

        End Using
    End Sub
    Protected Sub rptKetQua_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptKetQua.ItemDataBound
        Dim row As Menu = e.Item.DataItem
        Dim ltrTitle As Literal = e.Item.FindControl("ltrTitle")
        ltrTitle.Text = (e.Item.ItemIndex + 1).ToString & ". " & row.Title

        'load danh sach con neu co
        Using _data As New VSEntities
            Dim p = (From q In _data.Menus Where q.ParentId = row.MenuId Select q).ToList
            If p.Count > 0 Then
                Dim rptSub As Repeater = CType(e.Item.FindControl("rptSub"), Repeater)
                rptSub.DataSource = p
                rptSub.DataBind()
            Else
                Dim ltrDescription As Literal = e.Item.FindControl("ltrDescription")
                ltrDescription.Text = row.Description
            End If
        End Using

    End Sub
    Protected Sub rptSub_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As Menu = e.Item.DataItem
        Dim hplTitleSub As HyperLink = CType(e.Item.FindControl("hplTitleSub"), HyperLink)
        hplTitleSub.Text = "- " & row.Title

        Dim ltrDescriptionSub As Literal = CType(e.Item.FindControl("ltrDescriptionSub"), Literal)
        ltrDescriptionSub.Text = row.Description
    End Sub
    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtCode.Text = ""
        txtName.Text = ""
        Using _data As New VSEntities


            Dim p = (From q In _data.Menus Where q.TitlePage = "nganh-nghe-kinh-doanh-co-dieu-kien" Select q).FirstOrDefault
            If Not p Is Nothing Then

                Dim k = (From q In _data.Menus Where q.ParentId = p.MenuId Where (q.Title.Contains(txtCode.Text) And txtCode.Text <> "") Or (q.Title.Contains(txtName.Text) And txtName.Text <> "") _
                                                                                 Or (q.Description.Contains(txtCode.Text) And txtCode.Text <> "") Or (q.Description.Contains(txtName.Text) And txtName.Text <> "")
                                                                                  Select q Order By q.Title Descending).ToList

                With rptKetQua
                    .DataSource = k
                    .DataBind()
                End With
            End If
        End Using
    End Sub

End Class
